﻿using System;
using System.Collections.Generic;
using System.Text;
using EnCor.Security.Claims;

namespace EnCor.Security
{
    public class AuthorizationCenter
    {
        private AuthorizationProviderCollection _Providers;

        public AuthorizationCenter(AuthorizationConfig config, IdentityMapperCollection mappers)
        {
            List<IAuthorizationProvider> providers = new List<IAuthorizationProvider>();
            AuthorizationProviderFactory factory = new AuthorizationProviderFactory();
            foreach (AuthorizationProviderConfig providerConfig in config.Providers )
            {
                providers.Add( factory.Create(null, providerConfig) );
            }

            _Providers = new AuthorizationProviderCollection(providers);
        }

        public ClaimSet GetUserClaimSet(UserIdentity userIdentity)
        {
            return GetUserClaimSet(userIdentity.Id);
        }
        
        public ClaimSet GetUserClaimSet(EnCorIdentity identity)
        {
            return GetUserClaimSet(identity.UserId);
        }

        private ClaimSet GetUserClaimSet(string userId)
        {
            List<ClaimObject> claims = new List<ClaimObject>();
            foreach (IAuthorizationProvider provider in _Providers)
            {
                claims.AddRange(provider.AuthorizeIdentityRights(userId));
            }
            return new ClaimSet(claims);
        }
    }
}
